﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyQQ
{
    public partial class Frm_Main : Form
    {
        //消息发送者
        int formUserId;
        //发消息好友的头像ID
        int friendHeadId;
        //工具栏中消息图标的索引
        int messageImageIndex = 0;
        //自己的昵称
        public static string nickName = "";
        //在线状态
        public static string strFlag = "[离线]";
        //实例化数据库操作类的对象
        DataOperator dataOperator = new DataOperator();
        public Frm_Main()
        {
            InitializeComponent();
        }
    
        private void Frm_Main_CursorChanged(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 用来显示个人头像，昵称，账号，个性签名等
        /// </summary>
        public void ShowInfo()
        {
            //头像索引
            int headID = 0;
            //获取当前用户的昵称、头像
            string sql = "select NickName,HeadID,Sign from tb_User where ID = " + PublicClass.loginID;
            //执行查询操作
            SqlDataReader dataReader = dataOperator.GetDataRender(sql);
            var aaa= dataReader.Read();
            //读取查询结果
            if (aaa == true)
            {
                if (!(dataReader["NickName"]is DBNull))
                {
                    nickName = dataReader["NickName"].ToString();//记录自己的昵称
                }
                //记录自己的头像ID
                headID = Convert.ToInt32(dataReader["HeadID"]);
                //显示个性签名
                TxtSign.Text = dataReader["Sign"].ToString();
                //关闭读取器
                dataReader.Close();
                DataOperator.connection.Close();
                //设置窗体标题为当前登陆者的账号
                this.Text = PublicClass.loginID.ToString();
                //显示用户头像
                PboxHead.Image = ImgListHead.Images[headID];
                LblName.Text = nickName + "(" + PublicClass.loginID.ToString() + ")";
            }
        }

        /// <summary>
        /// 用来显示好友的头像
        /// </summary>
        public void ShowFriendList()
        {
            //清空原来的列标
            LvFriend.Items.Clear();
            //定义查找好友的sql语句
            string sql = "select FriendID ,NickName,HeadID,Flag from tb_User,tb_Friend where tb_Friend.HostID =" + PublicClass.loginID + "and tb_User.ID = tb_Friend.FriendID";
            SqlDataReader dataRender = dataOperator.GetDataRender(sql);
            int i = LvFriend.Items.Count;//记录添加到ListView中的项索引
            var aaa = dataRender.Read();
            //循环添加好友列表
            while (aaa == true)
            {
                if (dataRender["Flag"].ToString() == "0")
                    strFlag = "[离线]";
                else
                    strFlag = "[在线]";
                //记录好友昵称
                string strTemp = dataRender["NickName"].ToString();
                //对好友昵称进行处理
                string strFriendName = strTemp;
                if (strTemp.Length < 9)
                    strFriendName = strTemp.PadLeft(9, ' ');
                else
                    strFriendName = (strTemp.Substring(0, 2) + "...").PadLeft(9, ' ');
                LvFriend.Items.Add(dataRender["FriendID"].ToString(), strFriendName + strFlag, (int)dataRender["HeadID"]);
                LvFriend.Items[i].Group = LvFriend.Groups[0];
                i++;
                if (i == 1)
                {
                    return;
                }
            }
            dataRender.Close();
            DataOperator.connection.Close();
        }

        //显示个人信息及好友列表
        private void Frm_Main_Load(object sender, EventArgs e)
        {
            TsbtnMessageReading.Image = ImgListMessage.Images[0];
            ShowInfo();
            ShowFriendList();
        }
    }
}
